package cn.sdormitory.smartdor.service.impl;

import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.smartdor.dao.SdStudentHygieneDao;
import cn.sdormitory.smartdor.entity.SdHygieneStudent;
import cn.sdormitory.smartdor.service.SdStudentHygieneService;
import cn.sdormitory.smartdor.vo.SdStudentHygieneVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("sdStudentHygieneService")
public class SdStudentHygieneServiceImpl extends ServiceImpl<SdStudentHygieneDao, SdHygieneStudent> implements SdStudentHygieneService {

    @Resource
    private BStudentDao bStudentDao;

    @Resource
    private SdStudentHygieneDao sdStudentHygieneDao;

    @Override
    public int create(SdHygieneStudent sdHygieneStudent) {
        return this.baseMapper.insert(sdHygieneStudent);
    }

    @Override
    public int update(SdHygieneStudent sdHygieneStudent) {
        sdHygieneStudent.setModifyTime(new Date());
        return this.baseMapper.updateById(sdHygieneStudent);
    }

    /**
     * 根据宿舍id 和卫生考核时间获取学员考勤信息
     * @param bdormitoryId
     * @return
     */
    public List<SdStudentHygieneVo> getStuByCheckDateAndBdormitoryId(String accessDate, Long bdormitoryId){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            List<BStudent> studentList = bStudentDao.getBStudentByDormitoryId(bdormitoryId);

            List<SdStudentHygieneVo> studentHygieneList = new ArrayList<>();
            for (BStudent student : studentList) {
                SdStudentHygieneVo sdStudentHygieneVo = new SdStudentHygieneVo();
                sdStudentHygieneVo.setStudentId(student.getId());
                sdStudentHygieneVo.setStudentName(student.getStudentName());
                sdStudentHygieneVo.setStudentNo(student.getStudentNo());
                sdStudentHygieneVo.setClassId(student.getClassId());
                sdStudentHygieneVo.setClassName(student.getClassName());
                sdStudentHygieneVo.setPhone(student.getPhone());
                sdStudentHygieneVo.setIdcard(student.getIdcard());
                sdStudentHygieneVo.setBuildingNo(student.getBuildingNo());
                sdStudentHygieneVo.setStorey(student.getStorey());
                sdStudentHygieneVo.setBdormitoryId(student.getBdormitoryId());
                sdStudentHygieneVo.setDormitoryNo(student.getDormitoryNo());

                List<SdHygieneStudent> SdHygieneStudentList  = sdStudentHygieneDao.getStuByCheckDateAndBdormitoryId(accessDate,student.getStudentNo());
                //System.out.println("查询的信息： "+SdHygieneStudentList.size());
                if(SdHygieneStudentList!=null && SdHygieneStudentList.size()>0){
                    SdHygieneStudent sdHygieneStudent = SdHygieneStudentList.get(0);
                    sdStudentHygieneVo.setCheckDate(format.parse(sdHygieneStudent.getCheckDate()));
                    sdStudentHygieneVo.setGrade(sdHygieneStudent.getGrade());
                }else{
                    sdStudentHygieneVo.setCheckDate(format.parse(accessDate));
                    sdStudentHygieneVo.setGrade("0");
                }

                studentHygieneList.add(sdStudentHygieneVo);
            }
            return  studentHygieneList;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return  null;
    }

    /**
     * 增加学生卫生考核
     * @param sdStudentHygieneVo
     * @return
     */
    public int addStudentHygiene(SdStudentHygieneVo sdStudentHygieneVo){
        int count = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String[] studentNoArray = sdStudentHygieneVo.getStudentNoArray();
            Integer[] gradeArray = sdStudentHygieneVo.getGradeArray();
            if(studentNoArray!=null && studentNoArray.length>0) {
                for (int i = 0; i < studentNoArray.length; i++) {
                    SdHygieneStudent sdHygieneStudent = new SdHygieneStudent();
                    sdHygieneStudent.setStudentNo(studentNoArray[i]);
                    sdHygieneStudent.setCheckDate(format.format(sdStudentHygieneVo.getCheckDate()));
                    sdHygieneStudent.setGrade(gradeArray[i]+"");
                    sdHygieneStudent.setStorey(sdStudentHygieneVo.getStorey());
                    sdHygieneStudent.setBuildingNo(sdStudentHygieneVo.getBuildingNo());
                    sdHygieneStudent.setBdormitoryId(sdStudentHygieneVo.getBdormitoryId());
                    sdHygieneStudent.setDormitoryNo(sdStudentHygieneVo.getDormitoryNo());

                    //System.out.println("开始执行查询,检查时间： "+sdHygieneStudent.getCheckDate() +"\t  学生编号： "+sdHygieneStudent.getStudentNo());
                    //根据学号以及时间查询卫生考核记录
                    List<SdHygieneStudent> SdHygieneStudentList  = sdStudentHygieneDao.getStuByCheckDateAndBdormitoryId(sdHygieneStudent.getCheckDate(),sdHygieneStudent.getStudentNo());

                    if(SdHygieneStudentList==null || SdHygieneStudentList.size()<1){
                        sdStudentHygieneDao.insert(sdHygieneStudent);
                    }else{
                        sdStudentHygieneDao.updateStudentHygiene(sdHygieneStudent.getStudentNo(),sdHygieneStudent.getCheckDate(),sdHygieneStudent.getGrade());
                    }

                }
            }
            count = 1;
        }catch (Exception ex){
            ex.printStackTrace();
            count = 0;
        }

        return  count;
    }

}
